package code;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huang
 * @description
 * @date 2023/4/17 14:37
 * leetcode. 1
 */
public class Atest {

    public int longestCommonSubsequence(String text1, String text2) {

        return 1;
    }

    public static void main(String[] args) {

        String[] tool = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};


        System.out.println();
    }

    //第一种效率7.5%
    public double findMedianSortedArrays1(int[] nums1, int[] nums2) {
        List<Integer> collect1 = Arrays.stream(nums1).boxed().collect(Collectors.toList());
        List<Integer> collect2 = Arrays.stream(nums2).boxed().collect(Collectors.toList());
        collect1.addAll(collect2);
        collect1.sort((s1,s2)->{return s1-s2;});
        if((nums1.length+nums2.length)%2==1){
            return collect1.get((nums1.length+nums2.length+1)/2-1);
        }else {
            return (collect1.get((nums1.length+nums2.length)/2-1)+collect1.get((nums1.length+nums2.length)/2))/2.0;
        }
    }
    //第二种效率100%
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = 0;
        if ((nums1.length + nums2.length) % 2 == 1) {
            length = (nums1.length + nums2.length + 1) / 2;
        } else {
            length = (nums1.length + nums2.length) / 2 + 1;
        }
        int left = 0;
        int right = 0;
        for (int i = 0; i < length; i++) {
            if (left < nums1.length && right < nums2.length) {
                if (nums1[left] > nums2[right]) {
                    right++;
                } else {
                    left++;
                }
            } else if (left < nums1.length) {
                left++;
            } else {
                right++;
            }
        }
        left--;
        right--;
        int[] index = {left, right};
        if ((nums1.length + nums2.length) % 2 == 1) {
            return findmax(nums1, nums2, index);
        } else {
            int tem = findmax(nums1, nums2, index);
            return (tem + findmax(nums1, nums2, index)) / 2.0;
        }
    }

    private int findmax(int[] nums1, int[] nums2, int[] indexs) {
        if (indexs[0] >= 0 && indexs[1] >= 0) {
            if (nums1[indexs[0]] > nums2[indexs[1]]) {
                return nums1[indexs[0]--];
            } else {
                return nums2[indexs[1]--];
            }
        } else if (indexs[0] >= 0) {
            return nums1[indexs[0]--];
        } else {
            return nums2[indexs[1]--];
        }
    }
}
